// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   TradeEditor.java

package com.boci.clientapp.holding.ui.control;

import com.boci.clientapp.main.ClientAppLoader;
import com.boci.common.ui.control.CMURLButton;
import com.boci.common.util.MessagesConfigurator;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.*;
import java.util.Locale;
import javax.swing.*;
import javax.swing.table.TableCellEditor;

public class TradeEditor extends AbstractCellEditor
    implements TableCellEditor, ActionListener
{

    private void initGUI()
    {
        rootPanel.setBackground(Color.WHITE);
        FlowLayout layout = new FlowLayout(1, 0, -3);
        rootPanel.setLayout(layout);
        if(!sellOnlyFlag)
        {
            btnBuy = new CMURLButton();
            btnBuy.setMargin(new Insets(1, 1, 0, 0));
            btnBuy.setActionCommand("BUY");
            btnBuy.addActionListener(this);
            rootPanel.add(btnBuy);
            rootPanel.add(new JLabel("|"));
        }
        btnSell = new CMURLButton();
        btnSell.setMargin(new Insets(1, 1, 0, 0));
        btnSell.setActionCommand("SELL");
        btnSell.addActionListener(this);
        rootPanel.add(btnSell);
    }

    public void setText(Locale pLocale)
    {
        MessagesConfigurator msgConf = ClientAppLoader.getMessagesConfigurator();
        if(!sellOnlyFlag)
            btnBuy.setText(msgConf.getUIMessages("HOLDING.BUY", pLocale));
        btnSell.setText(msgConf.getUIMessages("HOLDING.SELL", pLocale));
        setFont(pLocale);
    }

    private void setFont(Locale pLocale)
    {
        if(!sellOnlyFlag)
            btnBuy.setFont(ClientAppLoader.getSmallPlainTextFont(pLocale));
        btnSell.setFont(ClientAppLoader.getSmallPlainTextFont(pLocale));
    }

    public TradeEditor(boolean pSellOnlyFlag, Locale pLocale)
    {
        rootPanel = new JPanel();
        strValue = null;
        sellOnlyFlag = pSellOnlyFlag;
        initGUI();
        setText(pLocale);
    }

    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column)
    {
        if(row % 2 == 0)
            rootPanel.setBackground(new Color(255, 255, 200));
        else
            rootPanel.setBackground(Color.WHITE);
        btnSell.setEnabled(true);
        if(!sellOnlyFlag)
            updateSellStatus(table.getValueAt(row, 3));
        else
            updateSellStatus(table.getValueAt(row, 3));
        return rootPanel;
    }

    private void updateSellStatus(Object pQtyValue)
    {
        if(pQtyValue == null)
        {
            btnSell.setEnabled(false);
        } else
        {
            NumberFormat numFormatter = new DecimalFormat("#,###,###");
            String strQty = (String)pQtyValue;
            if(strQty.length() == 0)
                btnSell.setEnabled(false);
            try
            {
                Number qty = numFormatter.parse(strQty.trim());
                if(qty.intValue() <= 0)
                    btnSell.setEnabled(false);
            }
            catch(ParseException pex)
            {
                btnSell.setEnabled(true);
            }
        }
    }

    public Object getCellEditorValue()
    {
        return strValue;
    }

    public void resetValue()
    {
        strValue = "";
    }

    public void actionPerformed(ActionEvent e)
    {
        strValue = e.getActionCommand();
        fireEditingStopped();
    }

    private JPanel rootPanel;
    private CMURLButton btnBuy;
    private CMURLButton btnSell;
    private String strValue;
    private boolean sellOnlyFlag;
}
